<template>
  <div class="risk">
    <div class="add" v-if="active == 1" @click="add()">创建证书</div>
    <van-search
      class="info_search input_text"
      v-model="value"
      @search="onSearch"
      @clear="clearSearch"
      placeholder="搜索"
    />
    <van-dropdown-menu>
      <van-dropdown-item
        v-model="value1"
        :options="option1"
        @change="handleTypeOneChange"
      :title="itemTitle "
      />
      <van-dropdown-item
        v-model="value2"
        :options="option2"
        @change="handleTypeTwoChange"
       :title="itemTitle2 "
      />
    </van-dropdown-menu>
    <template v-if="roles.indexOf('admin') != -1">
      <van-tabs v-model="active">
        <van-tab title="我的证书">
          <van-list
            style="margin: 10px"
            :finished="finished"
            v-model="loading"
            finished-text="没有更多了"
            @load="onLoad"
          >
            <van-cell
              style="margin-top: 10px"
              v-for="(item, index) in list"
              :key="index"
            >
              <van-card @click="handleClick(item.ID)">
                <template #thumb>
                  <!-- <img src="baseUrl + item.Attachs" alt=""> -->
                  <van-image
                    :src="baseUrl + item.Attachs"
                    class="image-box-shadow"
                    width="80"
                    height="80"
                  />
                </template>
                <template #title>
                  <!-- <img src="baseUrl + item.Attachs" alt=""> -->
                  <h3>{{ item.CertName }}</h3>
                </template>
                <template #desc>
                  <div>姓名：{{ item.ChnName }} {{ item.OrgName }}</div>
                  <div>下次复审日期：{{ item.NextReviewDate }}</div>
                </template>
              </van-card>
              <van-tag v-if="item.ExpireDays > 0" class="tags" type="warning"
                >逾期{{ Math.abs(item.ExpireDays) }}天</van-tag
              >
              <van-tag v-if="item.ExpireDays < 0" class="tags" color="#1989fa"
                >剩{{ Math.abs(item.ExpireDays) }}天</van-tag
              >
            </van-cell>
          </van-list>
        </van-tab>
        <van-tab title="我管理的证书">
          <van-list
            style="margin: 10px"
            :finished="myFinished"
            v-model="myLoading"
            finished-text="没有更多了"
            @load="myOnLoad"
          >
            <van-cell
              style="margin-top: 10px"
              v-for="(item, index) in myList"
              :key="index"
            >
              <van-card @click="handleClick(item.ID)">
                <template #thumb>
                  <!-- <img src="baseUrl + item.Attachs" alt=""> -->
                  <van-image
                    :src="baseUrl + item.Attachs"
                    class="image-box-shadow"
                    width="80"
                    height="80"
                  />
                </template>
                <template #title>
                  <!-- <img src="baseUrl + item.Attachs" alt=""> -->
                  <h3>{{ item.CertName }}</h3>
                </template>
                <template #desc>
                  <div>姓名：{{ item.ChnName }} {{ item.OrgName }}</div>
                  <div>下次复审日期：{{ item.NextReviewDate }}</div>
                </template>
              </van-card>
              <van-tag v-if="item.ExpireDays > 0" class="tags" type="warning"
                >逾期{{ Math.abs(item.ExpireDays) }}天</van-tag
              >
              <van-tag v-if="item.ExpireDays < 0" class="tags" color="#1989fa"
                >剩{{ Math.abs(item.ExpireDays) }}天</van-tag
              >
            </van-cell>
          </van-list>
        </van-tab>
        <van-tab title="我部门的证书">
          <van-list
            style="margin: 10px"
            :finished="finished"
            v-model="loading"
            finished-text="没有更多了"
            @load="onLoad"
          >
            <van-cell
              style="margin-top: 10px"
              v-for="(item, index) in deptList"
              :key="index"
            >
              <van-card @click="handleClick(item.ID)">
                <template #thumb>
                  <!-- <img src="baseUrl + item.Attachs" alt=""> -->
                  <van-image
                    :src="baseUrl + item.Attachs"
                    class="image-box-shadow"
                    width="80"
                    height="80"
                  />
                </template>
                <template #title>
                  <!-- <img src="baseUrl + item.Attachs" alt=""> -->
                  <h3>{{ item.CertName }}</h3>
                </template>
                <template #desc>
                  <div>姓名：{{ item.ChnName }} {{ item.OrgName }}</div>
                  <div>下次复审日期：{{ item.NextReviewDate }}</div>
                </template>
              </van-card>
              <van-tag v-if="item.ExpireDays > 0" class="tags" type="warning"
                >逾期{{ Math.abs(item.ExpireDays) }}天</van-tag
              >
              <van-tag v-if="item.ExpireDays < 0" class="tags" color="#1989fa"
                >剩{{ Math.abs(item.ExpireDays) }}天</van-tag
              >
            </van-cell>
          </van-list>
        </van-tab>
      </van-tabs>
    </template>
    <template v-else>
      <van-list
        style="margin: 10px"
        :finished="finished"
        v-model="loading"
        finished-text="没有更多了"
        @load="onLoad"
      >
        <van-cell
          style="margin-top: 10px"
          v-for="(item, index) in list"
          :key="index"
        >
          <van-card
            :title="item.CertName"
            :thumb="baseUrl + item.Attachs"
            @click="handleClick(item.ID)"
          >
            <template #desc>
              <div>姓名：{{ item.ChnName }} {{ item.OrgName }}</div>
              <div>下次复审日期：{{ item.NextReviewDate }}</div>
            </template>
          </van-card>
          <van-tag v-if="item.ExpireDays > 0" class="tags" type="warning"
            >逾期{{ Math.abs(item.ExpireDays) }}天</van-tag
          >
          <van-tag v-if="item.ExpireDays < 0" class="tags" color="#1989fa"
            >剩{{ Math.abs(item.ExpireDays) }}天</van-tag
          >
        </van-cell>
      </van-list>
    </template>
  </div>
</template>

<script>
import { list, getDict } from "@/api/cert";
import { Dialog } from "vant";
import { getDate } from "@/utils/getDate";
import { mapGetters } from "vuex";
export default {
  data() {
    return {
      baseUrl: process.env.VUE_APP_BASE_API,
      active: 0,
      value: "",
      list: [],
      myList: [],
      deptList: [],
      seletTypeOne: [],
      seletTypeTwo: [],
      selectKey: "",
      value1: "",
      value2: "",
      itemTitle: '证书状态',
      itemTitle2: '证书类别',
      option1: [
        { text: "已过期", value: "Expired" },
        { text: "三个月过期", value: "MonthsExpired" },
        { text: "正常", value: "Normal" },
      ],
      option2: [
        { text: "默认排序", value: "a" },
        { text: "好评排序", value: "b" },
        { text: "销量排序", value: "c" },
      ],
      myFilter: {
        page: 0,
        dataType: "MyManagerUserCert",
      },
      deptFilter: {
        page: 0,
        dataType: "MyDeptUserCert",
      },
      filter: {
        page: 0,
        dataType: "Mine",
      },
      loading: false,
      finished: false,
      myLoading: false,
      myFinished: false,
      deptLoading: false,
      deptFinished: false,
    };
  },
  beforeRouteLeave(to, from, next) {
    if (to.name == "CertEdit") {
      to.meta.title = "新增员工证书";
    }
    next();
  },
  created() {
    this.getList();
    this.getMyList();
    this.getDeptList();
    this.getDict();
  },
  computed: {
    ...mapGetters(["roles"]),
  },
  methods: {
    onSearch() {
      var conditions = [];
      if (this.value) {
        console.log("tttt");
        conditions.push({
          Name: "Keywords",
          DisplayName: this.value,
          Value: this.value,
          Operator: "like",
          DataType: "String",
        });
      }
      if (this.value1) {
        conditions.push({
          Name: "CertUserState",
          DisplayName: this.seletTypeOne[0].text,
          Value: this.seletTypeOne[0].value,
          Operator: "=",
          DataType: "String",
          IsTemp:false
        });
      }
      if (this.value2) {
        conditions.push({
          Name: "CertType",
          DisplayName: this.seletTypeTwo[0].text,
          Value: this.seletTypeTwo[0].value,
          Operator: "=",
          DataType: "String",
           IsTemp:false
        });
      }
      // conditions.forEach((item) => {
      //   item.DisplayName = this.value;
      //   item.Value = this.value;
      // });

        this.filter.page = 0;
        this.filter.conditions = JSON.stringify(conditions);
        this.getList();
       
        this.myFilter.page = 0;
        this.myFilter.conditions = JSON.stringify(conditions);
        this.getMyList();
      
        this.deptFilter.page = 0;
        this.deptFilter.conditions = JSON.stringify(conditions);
        this.getDeptList();
      
    },
    handleTypeOneChange(value) {
      this.seletTypeOne = [];
      this.option1.map((item) => {
        if (item.value == value) {
          this.itemTitle = item.text
          this.seletTypeOne.push(item);
        }
      });
      this.onSearch();
    },

    handleTypeTwoChange(value) {
      this.seletTypeTwo = [];
      this.option2.map((item) => {
        if (item.value == value) {
            this.itemTitle2 = item.text
          this.seletTypeTwo.push(item);
        }
      });
      this.onSearch();
    },
    // handleChange() {
    //   this.onSearch()
    //     //console.log('adasdasdad',);
    // },
    clearSearch() {
      this.filter = {
        page: 0,
        dataType: "Mine",
      };
      this.myFilter = {
        page: 0,
        dataType: "MyManagerUserCert",
      };
      this.deptFilter = {
        page: 0,
        dataType: "MyDeptUserCert",
      };
      this.value1 = "";
      this.value2 = "";
      this.value = "";
      this.getList();
      this.getMyList();
      this.getDeptList();
    },

    getDict() {
      this.loading = true;
      getDict()
        .then((res) => {
          this.option2 = [];
          res.data.map((item, index) => {
            this.option2.push({ text: item.Name, value: item.ID });
          });
          console.log(res, this.option2);
          this.loading = false;
        })
        .catch((err) => {
          console.log("err", err);
        });
    },
    getMyList() {
      this.myLoading = true;
      list(this.myFilter).then((res) => {
        this.myLoading = false;
        if (!res.data.HasNextPage) {
          this.myFinished = true;
        }
        res.data.Data.filter((item) => {
          item.NextReviewDate = getDate(item.NextReviewDate);
        });
        this.myList = res.data.Data;
        this.myFilter.page = res.data.PageIndex;
      });
    },
    getDeptList() {
      this.deptLoading = true;
      list(this.deptFilter).then((res) => {
        this.deptLoading = false;
        if (!res.data.HasNextPage) {
          this.myFinished = true;
        }
        res.data.Data.filter((item) => {
          item.NextReviewDate = getDate(item.NextReviewDate);
        });
        this.deptList = res.data.Data;
        this.deptFilter.page = res.data.PageIndex;
      });
    },
    getList() {
      this.loading = true;
      list(this.filter).then((res) => {
        this.loading = false;
        if (!res.data.HasNextPage) {
          this.finished = true;
        }
        res.data.Data.filter((item) => {
          item.NextReviewDate = getDate(item.NextReviewDate);
        });
        this.list = res.data.Data;
        this.filter.page = res.data.PageIndex;
      });
    },

    myOnLoad() {
      this.myLoading = true;
      ++this.myFilter.page;
      list(this.myFilter).then((res) => {
        this.myLoading = false;
        this.myFilter.page = res.data.PageIndex;
        res.data.Data.filter((item) => {
          item.NextReviewDate = getDate(item.NextReviewDate);
        });
        this.myList = this.myList.concat(res.data.Data);
        if (!res.data.HasNextPage) {
          this.myFinished = true;
        }
      });
    },
    deptOnLoad() {
      this.deptLoading = true;
      ++this.deptFilter.page;
      list(this.deptFilter).then((res) => {
        this.deptLoading = false;
        this.deptFilter.page = res.data.PageIndex;
        res.data.Data.filter((item) => {
          item.NextReviewDate = getDate(item.NextReviewDate);
        });
        this.deptList = this.deptList.concat(res.data.Data);
        if (!res.data.HasNextPage) {
          this.deptFinished = true;
        }
      });
    },

    onLoad() {
      this.loading = true;
      ++this.filter.page;
      list(this.filter).then((res) => {
        this.loading = false;
        this.filter.page = res.data.PageIndex;
        res.data.Data.filter((item) => {
          item.NextReviewDate = getDate(item.NextReviewDate);
        });
        this.list = this.list.concat(res.data.Data);
        if (!res.data.HasNextPage) {
          this.finished = true;
        }
      });
    },
    // 设备详情
    handleClick(certId) {
      this.$router.push({
        path: "/Credential/Detail",
        query: { certId: certId },
      });
    },
    getFile(equipmentCode) {
      this.$router.push({
        path: "/Equipment/safetyfile",
        query: { equipmentCode: equipmentCode },
      });
    },
    // 编辑设备信息
    add(equipmentCode) {
      this.$router.push({
        name: "CertEdit",
        params: { id: 0 },
      });
    },
    // 设备检验
    editCheck(equipmentCode) {
      this.$router.push({
        path: "/Equipment/editcheck",
        query: { equipmentCode: equipmentCode },
      });
    },
  },
};
</script>

<style lang="less" scoped>
.risk {
  height: 100vh;
  background-color: #e8f1f8;
  .info_search {
    padding: 10px;
    border-radius: 5px !important;
    .van-field__control {
      background-color: #ffffff !important;
    }
  }
}
.all_dep {
  background-color: #ffffff;
}
.van-cell {
  padding: 0 0;
  border-radius: 8px;
}
.warn_item {
  background-color: #ffffff;
  /*margin-bottom: 10px;*/
  padding: 10px 0 10px 10px;
  border-radius: 5px;
  position: relative;
}
.warn_header {
  display: flex;
  justify-content: space-between;
  position: relative;
  margin-bottom: 10px;
  .warn_tag {
    position: absolute;
    right: 0;
    top: 0;
    border-radius: 26.64rem 0 0 26.64rem;
  }
}
.dep_sort {
  display: flex;
  align-items: center;
  margin-bottom: 5px;
  img {
    height: 15px;
    width: 15px;
    margin-right: 5px;
  }
}
.dep_btm {
  display: flex;
  justify-content: flex-end;
  .btm_item {
    display: flex;
    align-items: center;
    img {
      height: 15px;
      width: 15px;
      margin-right: 5px;
    }
  }
}

.tags {
  position: absolute;
  right: 0;
  top: 10px;
  border-radius: 26.64rem 0 0 26.64rem;
}
</style>
